package com.example.administrator.basedemos.activity;

import android.view.View;
import android.widget.TextView;

import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.api.GankApi;
import com.example.administrator.basedemos.base.BaseActivity;
import com.example.administrator.basedemos.bean.AndroidDataBean;
import com.example.administrator.basedemos.bean.AppInfo;
import com.example.administrator.basedemos.bean.BlogBean;
import com.example.administrator.basedemos.bean.PostBean;
import com.example.administrator.basedemos.retorfit.RetrofitHelper;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.TmtUtils;
import com.example.net.bean.BaseBean;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.CompletableObserver;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * 创建者     ZCL
 * 创建时间   2018/3/20 15:12
 * 描述	      ${统一追加参数dao url}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class RetrofitEduRXActivity extends BaseActivity {

    private static final String TAG = "RetrofitEduActivity";
    @BindView(R.id.show_tv)
    TextView mShowTv;
    private GankApi mGankApi;

    @Override
    public void initView() {
        mGankApi = RetrofitHelper.getGank();
    }

    @Override
    public void initData() {
        /**
         * 上传文件
         */
        File file = new File("stroge");
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("loginToken", "zcl")
                .addFormDataPart("deviceToken", "zhang123")
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
                .build();

        mGankApi.uploadImg(requestBody)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean<AppInfo>>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean<AppInfo> appInfoBaseBean) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @OnClick({R.id.button_rx_get, R.id.button_rx_post, R.id.button_rx_completable, R.id.button_base_bean, R.id.button_base_bean_no})
    public void OnClik(View v) {
        switch (v.getId()) {
            case R.id.button_base_bean:
                //统1未剥离使用我们自定义的解析库GstGsonConverterFactory.create()
                mGankApi.rxGetNoWrapper("Android", 10, 1)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<BlogBean>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(List<BlogBean> listBaseBean) {
                                mShowTv.setText(listBaseBean.get(2).getDesc() + "----" + listBaseBean.size());
                            }

                            @Override
                            public void onError(Throwable e) {
                                LogUtil.util(TAG, "onerror:" + e.getLocalizedMessage());
                            }

                            @Override
                            public void onComplete() {
                                LogUtil.util(TAG, "onComplete:");

                            }
                        });
                break;
            case R.id.button_base_bean_no:
                //统1未剥离使用GsonConverterFactory.create()
                mGankApi.rxGetByWrapper("Android", 10, 1)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<BaseBean<List<BlogBean>>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(BaseBean<List<BlogBean>> listBaseBean) {
                                String desc = listBaseBean.getResults().get(2).getDesc();
                                mShowTv.setText(desc);
                                LogUtil.util(TAG, "onNext:" +desc);
                            }

                            @Override
                            public void onError(Throwable e) {
                                LogUtil.util(TAG, "onerror:" + e.getLocalizedMessage());
                            }

                            @Override
                            public void onComplete() {
                                LogUtil.util(TAG, "onComplete:");

                            }
                        });
                break;
            case R.id.button_rx_completable:
                mGankApi.rxCompletable("http://gank.io/api/data/Android/10/1", "测试post",
                        "zcl", "Android", "true")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CompletableObserver() {

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onComplete() {
                                TmtUtils.midToast(RetrofitEduRXActivity.this, "上传成功!");
                                mShowTv.setText("上传成功com");
                                LogUtil.util(TAG, "上传成功!");
                            }

                            @Override
                            public void onError(Throwable e) {
                                mShowTv.setText("上传失败com");
                                TmtUtils.midToast(RetrofitEduRXActivity.this, "上传失败!");
                                LogUtil.util(TAG, "上传失败!");

                            }
                        });
                break;
            case R.id.button_rx_post:
                mGankApi.rxPost("http://gank.io/api/data/Android/10/1", "测试post",
                        "zcl", "Android", "true")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<PostBean>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(PostBean o) {
                                String msg = o.getMsg();
                                mShowTv.setText(msg);
                                LogUtil.util(TAG, "onNext:" +msg);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

                break;
            case R.id.button_rx_get:
                mGankApi.rxGet("Android", 10, 1)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<AndroidDataBean>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                LogUtil.util(TAG, "onSubscribe:");

                            }

                            @Override
                            public void onNext(AndroidDataBean androidDataBean) {
                                LogUtil.util(TAG, "onNext返回数据是:" + androidDataBean.toString());
                                mShowTv.setText(androidDataBean.getResults().get(0).getDesc());
                            }

                            @Override
                            public void onError(Throwable e) {
                                LogUtil.util(TAG, "onerror:" + e.getLocalizedMessage());
                            }

                            @Override
                            public void onComplete() {
                                LogUtil.util(TAG, "onComplete:");

                            }
                        });
                break;
            default:
                break;
        }
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_edu_rx;
    }
}